package com.host.dyyl.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author
 * @describe
 * @fileName MyHttpUtils
 * @date 2017/12/20 20:06
 */

public class MyHttpUtils {
    public static String toGet(String path) {
        //get的方式提交就是url拼接的方式
        try {
            String resultData = "";
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(5000);
            connection.setRequestMethod("GET");
            //获得结果码
            int responseCode = connection.getResponseCode();
            if (responseCode == 200) {
                //请求成功 获得返回的流
                InputStream is = connection.getInputStream();
//                return IOSUtil.inputStream2String(is);
                // 为输出创建BufferedReader
                InputStreamReader in = new InputStreamReader(
                        is);
                BufferedReader buffer = new BufferedReader(in);
                String inputLine = null;
                // 使用循环来读取获得的数据
                while (((inputLine = buffer.readLine()) != null)) {
                    // 我们在每一行后面加上一个"\n"来换行
                    resultData += inputLine + "\n";
                }
                // 关闭InputStreamReader
                in.close();
                // 关闭http连接
                connection.disconnect();
                return resultData;
            } else {
                //请求失败
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
